<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
  /**
 * Arbou CMS System
 *
 * 内容管理系统
 * 
 * 新闻分类
 *
 * @subpackage	Controller
 * @category	Admin Controller 
 *
 * @package		Arbou Cms
 * @author		Arbou <arbou.guo@qq.com>
 * @copyright	Copyright (c) 2010 - 2011, arbou.com.
 * @link		http://www.arbou.cn
 * @version		$versionid
 */
class Category extends Arbou_Auth_Controller {
	
	/**
     * 传递到对应视图的数据
     *
     * @access private
     * @var array
     */
	private $_data = array();
	
	/**
     * 当前操作类别ID
     *
     * @access private
     * @var int
     */
	private $_cid = 0;
	
	/**
     * 当前操作Meta类型
     *
     * @access private
     * @var string
     */
	private $_type = 'category';

	public function __construct()
	{
		parent::__construct();
		
		/** privilege confirm */
		$this->auth->exceed('editor');
		
		/** 标题 */
		$this->_data['page_title'] = '分类与标签';
		$this->_data['parentPage'] = 'manage-posts';
		$this->_data['currentPage'] = 'manage-metas';
	}
	
	/**
     * 默认执行函数
     *
     * @access public
     * @return void
     */
	public function index()
	{
		redirect('admin/category/manage');
	}
	
	/**
     * 添加一个新闻类别(与用户交互)
     *
     * @access private
     * @return void
     */
	private function _write()
	{
		/** set title */
		$this->_data['page_title'] = '撰写新文章';
		
		/** populated data 
		$this->_data['all_categories'] = $this->metas_mdl->list_metas('category');
		$this->_data['all_tags'] = $this->metas_mdl->list_metas('tag');
		$this->_data['attachments'] = $this->posts_mdl->get_posts('attachment','unattached',$this->user->uid,100,0);
		$this->_data['allow_comment'] = 1;
		$this->_data['allow_ping'] = 1;
		$this->_data['allow_feed'] = 1;*/
		
		/** validation rules */
		$this->_load_validation_rules();
		
		/** validation passed or failed? */
		if($this->form_validation->run() === FALSE)
		{
			/** validation failed */
			
			$this->load->view('admin/news_category_add',$this->_data);
		}
		else
		{
			$this->_insert_post();
		}
	}
	
	/**
     * 函数转发
     *
     * @access public
     * @return void
     */
	public function write()
	{
		if (FALSE === $this->uri->segment(4))
		{
			$this->_write();
		}
		else
		{
			$pid = $this->input->xss_clean($this->uri->segment(4));
			is_numeric($pid)?$this->_edit($pid):show_error('禁止访问：危险操作');
		}
	}
	
	/**
     * 管理页面
     *
     * @access public
     * @param  string $type 类型
     * @param  int 	  $mid  ID
     * @return void
     */
	public function manage($cid = NUll)
	{
		//$this->_data['type'] = $type;
		//$this->_data[$type] = $this->metas_mdl->list_metas($type);

		if($cid && is_numeric($cid))
		{
			$this->_data['cid'] = $cid;
			
			$meta = $this->metas_mdl->get_meta('BYID', $mid);
			
			$this->_data['name'] = $meta->name;
			$this->_data['slug'] = $meta->slug;
			$this->_data['description'] = $meta->description;
			
			unset($meta);
		}
		
		//$this->_operate($type, $mid);
		
		$this->load->view('admin/news_category_list',$this->_data);
	}
	
	/**
     * 加载验证规则
     *
     * @access private
     * @return void
     */
	private function _load_validation_rules()
	{
		$this->form_validation->set_rules('name', '名称', 'required|trim|callback__name_check|callback__name_to_slug|htmlspecialchars');
		
		if('category' == $this->_type)
		{
			$this->form_validation->set_rules('slug', '缩略名', 'trim|callback__slug_check|alpha_dash|htmlspecialchars');
		}
		else
		{
			$this->form_validation->set_rules('slug', '缩略名', 'trim|callback__slug_check|htmlspecialchars');	
		}
		
		$this->form_validation->set_rules('description', '描述', 'trim|htmlspecialchars');	
	}

	/**
     * 操作分发
     *
     * @access public
     * @param  string $type 类型
     * @return void
     */
	public function operate($type)
	{
		$action = $this->input->post('do',TRUE);
		
		switch ($action)
		{
			case 'delete':
					$this->_remove($type);
					break;
			case 'refresh':
					$this->_refresh($type);
					break;
			case 'merge':
					$this->_merge($type);
					break;
			default:
					show_404();
					break;
		}
	}

	/**
     * 删除
     *
     * @access private
     * @param  string $type 类型
     * @return void
     */
	private function _remove($type)
	{
		$metas = $this->input->post('mid',TRUE);
        $deleted = 0;
        
        if ($metas && is_array($metas)) 
        {
            foreach ($metas as $meta) 
            {
                if($this->metas_mdl->remove_meta($meta))
                {
                	$this->metas_mdl->remove_relationship('mid',$meta);
                	$deleted ++;
                }
            }
        }
        
        $msg = ($deleted>0) ? $this->_map[$type].'删除成功' : '没有'.$this->_map[$type].'被删除';
        $notify = ($deleted>0) ? 'success':'error';
        
        $this->session->set_flashdata($notify, $msg);
		go_back();
	}

	/**
     * 刷新
     *
     * @access private
     * @param  string $type 类型
     * @return void
     */
	private function _refresh($type)
	{
		$metas = $this->input->post('mid',TRUE);
        
        if ($metas && is_array($metas)) 
        {
            foreach ($metas as $meta) 
            {
				$this->metas_mdl->refresh_count($meta, 'post', 'publish');
            }
            
            $this->session->set_flashdata('success', '分类刷新已经完成');
        }
        else
        {
        	$this->session->set_flashdata('error', '没有选择任何分类');
        }
        
        go_back();
	}
	
	 /**
     * 回调函数：检查Name是否唯一
     * 
     * @access public
     * @param $str 输入值
     * @return bool
     */
	public function _name_check($str)
	{
		if($this->metas_mdl->check_exist($this->_type, 'name', $str, $this->_mid))
		{
			$this->form_validation->set_message('_name_check', '已经存在一个为 '.$str.' 的名称');
			
			return FALSE;
		}
		
		return TRUE;
	}
	
	 /**
     * 回调函数：检查Slug是否唯一
     * 
     * @access public
     * @param $str 输入值
     * @return bool
     */
	public function _slug_check($str)
	{
		if($this->metas_mdl->check_exist($this->_type, 'slug', Common::repair_slugName($str), $this->_mid))
		{
			$this->form_validation->set_message('_slug_check', '已经存在一个为 '.$str.' 的缩略名');
			
			return FALSE;
		}
		
		return TRUE;
	}
	
	 /**
     * 回调函数：名称转化为缩略名
     * 
     * @access public
     * @param $str 输入值
     * @return bool
     */
	public function _name_to_slug($str)
	{
		$slug = Common::repair_slugName($str);
		
        if(empty($slug) || $this->metas_mdl->check_exist($this->_type, 'slug',$slug, $this->_mid)) 
        {
        	$this->form_validation->set_message('_name_to_slug', '分类无法转换为缩略名');
        	return FALSE;
        }
        
        return TRUE;
	}
	
}

/* End of file Metas.php */
/* Location: ./application/controllers/admin/Metas.php */